package org.study.springboot.schedule;

import com.alibaba.fastjson.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.ResponseEntity;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;
import org.study.springboot.model.tonghuashun.*;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

@Component
@Configuration      //1.主要用于标记配置类，兼备Component的效果。
@EnableScheduling   //2.开启定时任务
public class QueryDataFromTonghuashun {

    private static final  Logger logger = LoggerFactory.getLogger(QueryDataFromTonghuashun.class);

    @Autowired
    private RestTemplate restTemplate;

    private static String baseUri= "https://data.10jqka.com.cn/dataapi/limit_up";

   // @Scheduled(cron = "0/5 * * * * ?")
    private void LowerLimitPools() {
        System.err.println("执行跌停池任务: " + LocalDateTime.now().toLocalTime());
        StringBuilder dieTingPoolsUrl = new StringBuilder(baseUri);
        dieTingPoolsUrl.append("/lower_limit_pool?page=1&limit=150&field=199112,10,330333,330334,1968584,3475914,9004&filter=HS,GEM2STAR&order_field=330334&order_type=0");
        dieTingPoolsUrl.append("&date=").append(format()).append("&_=").append(System.currentTimeMillis());
        System.out.println(dieTingPoolsUrl.toString());
        ResponseEntity<LowerLimitPool> forEntity = restTemplate.getForEntity(dieTingPoolsUrl.toString(), LowerLimitPool.class);
        LowerLimitPool body = forEntity.getBody();
        String toJSONString = JSONObject.toJSONString(body);
        System.out.println(toJSONString);
    }
   // @Scheduled(cron = "0/5 * * * * ?")
    private void OpenLimitPools() {
        System.err.println("炸板池 " + LocalDateTime.now().toLocalTime());
        System.err.println("执行炸板池任务: " + LocalDateTime.now().toLocalTime());
        StringBuilder url = new StringBuilder(baseUri);
        url.append("/open_limit_pool?page=1&limit=150&field=199112,9002,48,1968584,19,3475914,9003,10,9004&filter=HS,GEM2STAR&order_field=199112&order_type=0");
        url.append("&date=").append(format()).append("&_=").append(System.currentTimeMillis());
        System.out.println(url.toString());
        ResponseEntity<OpenLimitPool> forEntity = restTemplate.getForEntity(url.toString(), OpenLimitPool.class);
        OpenLimitPool body = forEntity.getBody();
        String toJSONString = JSONObject.toJSONString(body);
        System.out.println(toJSONString);
    }
   // @Scheduled(cron = "0/5 * * * * ?")
    private void ContinuousLimitPool() {
        System.err.println("连板池 " + LocalDateTime.now().toLocalTime());
        System.err.println("执行炸板池任务: " + LocalDateTime.now().toLocalTime());
        StringBuilder url = new StringBuilder(baseUri);
        url.append("/continuous_limit_pool?page=1&limit=15&field=199112,10,330329,330325,133971,133970,1968584,3475914,3541450,9004&filter=HS,GEM2STAR&order_field=330329&order_type=0");
        url.append("&date=").append(format()).append("&_=").append(System.currentTimeMillis());
        System.out.println(url.toString());
        ResponseEntity<ContinuousLimitPool> forEntity = restTemplate.getForEntity(url.toString(), ContinuousLimitPool.class);
        ContinuousLimitPool body = forEntity.getBody();
        String toJSONString = JSONObject.toJSONString(body);
        System.out.println(toJSONString);
    }

   // @Scheduled(cron = "0/5 * * * * ?")
    private void LimitUp() {
        System.err.println("冲刺涨停 " + LocalDateTime.now().toLocalTime());
        System.err.println("冲刺涨停任务: " + LocalDateTime.now().toLocalTime());
        StringBuilder url = new StringBuilder(baseUri);
        url.append("/limit_up?page=1&limit=2&field=199112,10,48,1968584,19,3475914,9003,9004&filter=HS,GEM2STAR&order_field=199112&order_type=0");
        url.append("&date=").append(format()).append("&_=").append(System.currentTimeMillis());
        System.out.println(url.toString());
        ResponseEntity<LimitUp> forEntity = restTemplate.getForEntity(url.toString(), LimitUp.class);
        LimitUp body = forEntity.getBody();
        String toJSONString = JSONObject.toJSONString(body);
        System.out.println(toJSONString);
    }
    //@Scheduled(cron = "0/5 * * * * ?")
    private void LimitUpPool() {
        System.err.println("涨停池" + LocalDateTime.now().toLocalTime());
        System.err.println("涨停池任务: " + LocalDateTime.now().toLocalTime());
        StringBuilder url = new StringBuilder(baseUri);
        url.append("/limit_up_pool?page=1&limit=15&field=199112,10,9001,330323,330324,330325,9002,330329,133971,133970,1968584,3475914,9003,9004&filter=HS,GEM2STAR&order_field=330324&order_type=0");
        url.append("&date=").append(format()).append("&_=").append(System.currentTimeMillis());
        System.out.println(url.toString());
        ResponseEntity<LimitUpPool> forEntity = restTemplate.getForEntity(url.toString(), LimitUpPool.class);
        LimitUpPool body = forEntity.getBody();
        String toJSONString = JSONObject.toJSONString(body);
        System.out.println(toJSONString);
    }
   // @Scheduled(cron = "0/5 * * * * ?")
    private void BlockTop() {
        System.err.println("最强风口" + LocalDateTime.now().toLocalTime());
        System.err.println("最强风口任务: " + LocalDateTime.now().toLocalTime());
        StringBuilder url = new StringBuilder(baseUri);
        url.append("/block_top?filter=HS,GEM2STAR");
        url.append("&date=").append(format());
        System.out.println(url.toString());
        ResponseEntity<BlockTop> forEntity = restTemplate.getForEntity(url.toString(), BlockTop.class);
        BlockTop body = forEntity.getBody();
        String toJSONString = JSONObject.toJSONString(body);
        System.out.println(toJSONString);
    }
   // @Scheduled(cron = "0/5 * * * * ?")
    private void ContinuousLimitUp() {
        System.err.println("连板天梯" + LocalDateTime.now().toLocalTime());
        System.err.println("连板天梯任务: " + LocalDateTime.now().toLocalTime());
        StringBuilder url = new StringBuilder(baseUri);
        url.append("/continuous_limit_up?filter=HS,GEM2STAR");
        url.append("&date=").append(format());
        System.out.println(url.toString());
        ResponseEntity<ContinuousLimitUp> forEntity = restTemplate.getForEntity(url.toString(), ContinuousLimitUp.class);
        ContinuousLimitUp body = forEntity.getBody();
        String toJSONString = JSONObject.toJSONString(body);
        System.out.println(toJSONString);
    }



    public static String format() {
        LocalDate now = LocalDate.now();
        DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyyMMdd");
        String format = now.format(dtf);
        return format;
    }


}
